//
//  LYEmoticonKeyBoardView.swift
//  EmoticonKeyboard
//
//  Created by liuerying on 16/5/30.
//  Copyright © 2016年 liuerying. All rights reserved.
//

import UIKit
import SnapKit

private let toolBarHeight:CGFloat = 37
private let keyboardViewHeight:CGFloat = 220

private let emoticonItemCellID = "emoticonItemCellID"
class LYEmoticonKeyBoardView: UIView {

    lazy var packages :[LYEmoticonPackage] = LYEmoticonManager.sharedEmoticonTools.packages
    
    override init(frame: CGRect) {
        let rect = CGRect(x: 0, y: 0, width: 0, height: keyboardViewHeight)
        super.init(frame: rect)

        self.collectionView.registerClass(LYEmoticonCell.self, forCellWithReuseIdentifier: emoticonItemCellID)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置UI界面
    private func setupUI(){
//        backgroundColor = UIColor.yellowColor()
        addSubview(toolBar)
        
        addSubview(collectionView)
        addSubview(pageControl)
        
        //设置约束
        toolBar.snp_makeConstraints { (make) in
            make.left.right.bottom.equalTo(self)
            //设置高度
            make.height.equalTo(toolBarHeight)
        }
        
        collectionView.snp_makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)

        }
        
        pageControl.snp_makeConstraints { (make) in
            make.left.right.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top).offset(-5)
        }
        
        //MARK: 实现视图联动
        //制备好闭包  -
        toolBar.selectedIndexCallBack = {(index:Int) -> () in
        
//            print(index)
            
            let indexPath = NSIndexPath(forItem: 0, inSection: index)
            
            //移动到底index组
            self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
            //让pagecoller跟着一起连动
            self.updatePageControl(indexPath)
            
        }
        
        dispatch_async(dispatch_get_main_queue()) { 
            let indexPath = NSIndexPath(forItem: 0, inSection: 0)
        
            self.updatePageControl(indexPath)
        }
    }
    
    //懒加载collectionView 表情视图
    private lazy var collectionView: UICollectionView = {
    
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSizeMake(UIScreen.mainScreen().bounds.width, keyboardViewHeight - toolBarHeight)
        
        layout.scrollDirection = .Horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        
        let vc = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        
        vc.dataSource = self
        vc.delegate = self
    
        vc.pagingEnabled = true
        vc.bounces = false
        
       return vc
        
    }()
    
    //懒加载

    private lazy var toolBar: LYEmoticonToolBar = LYEmoticonToolBar()

    private lazy var pageControl:UIPageControl = {
    
        let p = UIPageControl()
        
        p.numberOfPages = 6
        
        p.currentPage = 4
         p.userInteractionEnabled = false
        
        p.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "_pageImage")
        p.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "_currentPageImage")
    
        return p
    }()
}


//数据源方法
extension LYEmoticonKeyBoardView: UICollectionViewDataSource,UICollectionViewDelegate{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return packages.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return packages[section].sectionEmoticon.count

    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
//        backgroundColor = UIColor.redColor()
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(emoticonItemCellID, forIndexPath: indexPath) as! LYEmoticonCell
        
//        cell.backgroundColor = randomColor()
        
//        cell.indexPath = indexPath
        
        cell.emoticons = packages[indexPath.section].sectionEmoticon[indexPath.item]
        return cell
    }
    
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        var center = collectionView.center
        
        center.x = center.x + collectionView.contentOffset.x
        
        let indexPaths = collectionView.indexPathsForVisibleItems()
     
        for indexPath in indexPaths{
        
            let cell = collectionView.cellForItemAtIndexPath(indexPath)
            
            if cell!.frame.contains(center) {
            
                updatePageControl(indexPath)
                
                toolBar.setselectedBtn(indexPath.section)
            }
        }
    }
    
    private func updatePageControl(indexPath:NSIndexPath) {
    
        pageControl.numberOfPages = packages[indexPath.section].sectionEmoticon.count

        pageControl.currentPage = indexPath.item
    
    }
}
